<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head th:replace="admin/_fragments :: head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>

    <!--    预览路径 ,可以直接在 IDEA 打开 。如果不导入两遍就预览不了 -->
    <link rel="icon" href="../../static/favicon.ico" type="image/x-icon">
    <!--导入网页图标 -->
    <link rel="shortcut icon" href="../../static/favicon.ico" type="image/x-icon">
    <!--导入网页图标 -->
    <script src="../../static/lib/jQuery/jquery-3.5.1.min.js"></script>
    <!--导入jQuery库 -->
    <script src="../../static/lib/layui/layui.js"></script>
    <!--导入layUI库 -->
    <script src="../../static/lib/cookie/js.cookie-2.2.1.min.js"></script>
    <!--导入cookie库 -->
    <link rel="stylesheet" href="../../static/lib/layui/css/layui.css" media="all">
    <!--导入layUI css库 -->
    <link rel="stylesheet" href="../../static/css/common.css">
    <!--导入公共的css，其中可定义整个网页的配色方案 -->

</head>

<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <!-- 页面的header -->
    <div th:replace="admin/_fragments :: admin-header" class="layui-header">
        <div class="layui-logo">图书管理系统</div>
        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item"><a class="layui-icon layui-icon-shrink-right" href="javascript:void(0)"></a>
            </li>


        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:void(0)">
                    <img src="../../static/images/ashini.ico" class="layui-nav-img">
                    啊、是你
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="/admin/login">安全退出</a></li>
        </ul>
    </div>
    <!-- 页面的menu/左边的导航栏 -->
    <div th:replace="admin/_fragments :: admin-menu" id="div_side_bg" class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item">
                    <a class="" href="javascript:void(0)">用户管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:void(0)">用户列表</a></dd>
                        <dd><a href="javascript:void(0)">权限列表</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item  layui-nav-itemed">
                    <a href="javascript:void(0)">图书管理</a>
                    <dl class="layui-nav-child">
                        <dd class="layui-this"><a href="javascript:void(0)" th:href="@{/admin/book-list}">图书列表</a></dd>
                        <dd><a href="javascript:void(0)">图书类别列表</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a class="" href="javascript:void(0)">借阅管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:void(0)">列表一</a></dd>
                        <dd><a href="javascript:void(0)">列表二</a></dd>
                        <dd><a href="javascript:void(0)">列表三</a></dd>
                    </dl>
                </li>
                <!--                    <li class="layui-nav-item"><a href="">云市场</a></li>-->
                <!--                    <li class="layui-nav-item"><a href="">发布商品</a></li>-->
            </ul>
        </div>
    </div>
    <div class="layui-body">
        <!-- 内容主体区域 -->

        <div style="width: 100%;height:98%;padding-left: 15px;padding-top: 10px">

            <!-- 面包屑-->
            <span class="layui-breadcrumb" lay-separator=" | ">
                <a href="/admin/index">首页</a>
                <a href="/user/admin/user-list">用户管理</a>
                <a><cite>用户列表</cite></a>
            </span>
            <div id="search_div" style="margin-top: 10px">

                <div class="layui-inline">
                    <input class="layui-input" name="searchUserId" placeholder="请输入用户编号" id="search_by_id"
                           autocomplete="off" disabled="disabled" style="background-color: #e6e6e6;">
                </div>

                <div class="layui-inline">
                    <input class="layui-input" name="searchUsername" placeholder="请输入用户名的关键词" id="search_by_key_word"
                           autocomplete="off">
                </div>


                <div class="layui-inline">
                    <select class="layui-input" id="search_by_select" style="width: 200px;" >
                        <option value="-1" selected="selected">不限类别</option>
                        <option value="" th:each="role:${roleList}" th:value="${role.roleId}" th:text="${role.rolename}">数学类</option>
                    </select>
                </div>


                <div class="layui-inline">
                    <select id="how_to_search" class="layui-input" style="width: 200px">
                        <option value="0" >精确查询</option>
                        <option value="1" selected="selected">模糊查询</option>
                    </select>
                </div>

                <button class="layui-btn" id="search_by_user_name_btn" data-type="reload">
                    <i class="layui-icon layui-icon-search"></i>
                </button>
            </div>
            <!-- layUI渲染的表格组件 -->
            <table class="layui-hide" id="test" lay-filter="test"></table>

            <!-- 表格组件上方的按钮 -->
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">批量删除</button>
                    <button class="layui-btn layui-btn-sm" lay-event="insert_new_user">添加用户</button>
                </div>
            </script>

            <!-- 表格组件内部的操作按钮 -->
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>

        </div>
    </div>

    <div th:replace="admin/_fragments :: admin-footer" class="layui-footer">
        <!-- 底部固定区域 -->
        ©2020 版权归武汉理工大学计算机1702"我都行，你们商量吧"小组所有&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <span style="color:rgb(144, 145, 146)">
                技术支持：https://www.layui.com/
            </span>
    </div>
</div>


<!--导入公共的js，其中实现了菜单按钮的点击事件-->
<script src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
<!-- layUI执行脚本,预先加载 -->
<script>
    layui.use(['element', 'table'], function () {
        var table = layui.table;
        table.render({
            elem: '#test',
            url: "/user/all",
            limit: 10,
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: ['filter', 'exports', 'print'],
            title: '用户数据表',
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'userId',
                    title: '用户编号',
                    width: 100
                }, {
                    field: 'roleName',
                    title: '角色',
                    width: 120,
                    sort: true
                }, {
                    field: 'username',
                    title: '用户名',
                    width: 100
                }, {
                    field: 'password',
                    title: '密码',
                    width: 160
                }, {
                    field: 'phone',
                    title: '联系方式',
                    width: 160
                }, {
                    field: 'address',
                    title: '地址',
                    width: 160
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150
                }]
            ],
            page: true,
            id: 'table_id'
        });

        //表格渲染重载,通过where字段传递查询条件 searchName
        var $ = layui.$;

        $("#how_to_search").change(function () {
            var $searchById = $("#search_by_id");
            var $searchByKeyWord = $("#search_by_key_word");
            var $searchBySelect = $("#search_by_select");
            console.log($(this).val())
            if($(this).val()==="0"){
                console.log("精确查询")
                //精确查询
                $searchById.val("");
                $searchById.removeAttr("disabled");
                $searchById.css("background-color","#ffffff");

                $searchByKeyWord.val("");
                $searchByKeyWord.attr("disabled","disabled");
                $searchByKeyWord.css("background-color","#e6e6e6");

                $searchBySelect.val("-1");
                $searchBySelect.attr("disabled","disabled");
                $searchBySelect.css("background-color","#d2d2d2");

            }else{
                console.log("模糊查询")
                //模糊查询
                $searchById.val("");
                $searchById.attr("disabled","disabled");
                $searchById.css("background-color","#e6e6e6");

                $searchByKeyWord.val("");
                $searchByKeyWord.removeAttr("disabled");
                $searchByKeyWord.css("background-color","#ffffff");

                $searchBySelect.val("-1");
                $searchBySelect.removeAttr("disabled");
                $searchBySelect.css("background-color","#ffffff");

            }
        });

        var active = {
            reload: function () {
                //执行重载
                table.reload('table_id', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        //查询条件
                        queryParam:{
                            userId:$("#search_by_id").val(),
                            keyWord:$("#search_by_key_word").val(),
                            roleId:$("#search_by_select").val(),
                            queryType:$("#how_to_search").val()
                        }
                        //这里可以传对象
                    }
                });
            }
        };

        //搜索框点击事件
        $('#search_by_user_name_btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    if(data.length===0){
                        layer.msg("请选择要删除的用户");
                    }else{
                        var names= [];
                        data.forEach(user=>names.push(user.username));
                        layer.confirm("确定要删除'"+names+"'这些图书?",function () {
                            var ids = "";
                            data.forEach(user=>ids=ids+","+user.userId);
                            if(ids.length>=2){
                                ids = ids.substring(1);
                            }
                            window.location.href = "/user/deleteAll?ids="+ids;
                        })
                    }
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                case 'insert_new_user':
                    window.location.href = "/user/admin/user-input?userId=-1";
                    break;
            }

        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('确定要删除用户' + data.username , function (index) {
                    window.location.href = "/user/delete?userId=" + data.userId;
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                window.location.href = "/user/admin/user-input?userId=" + data.userId;
            }
        });
    });
</script>

<!--author : 计算机1702  zhao fan -->
</body>

</html>